/*
 * @FilePath: App.java
 * @Author: lolan b1723552783@outlook.com
 * @Date: 2022-10-03 11:32:18
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-10 18:25:37
 * Copyright: 2022 xxxTech CO.,LTD. All Rights Reserved.
 * @Descripttion: 
 */
package com.lolan.training;

import java.io.*;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;


import com.lolan.training.tools.IOUtils;
import okhttp3.*;


//import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Hello world!
 *
 */
public class HMACApp 
{
//    private static ObjectMapper MAPPER = new ObjectMapper();
    // public static String getHtmlByPost(String urlString,String postBody) throws IOException {
    //     OkHttpClient client = new OkHttpClient().newBuilder()
    //         .build();
    //     MediaType mediaType = MediaType.parse("application/json");
    //     RequestBody body = RequestBody.create(mediaType, "{\"jsonrpc\": \"2.0\",\"method\": \"call\",\"params\": {\r\n        \"model\": \"ps.workflow.server\",\r\n        \"method\": \"p_exec_services\",\r\n        \"data\": {\r\n            \"bill_id\": 8132,\r\n            \"services\": [\r\n                {\r\n                    \"function\": \"svc_std_pre_audit\",\r\n                    \"model\": \"studio.base\"\r\n                }\r\n            ],\r\n            \"model_id\": \"iv.input.vat.inv9\"\r\n        }\r\n    },\r\n    \"id\": null\r\n}");
    //     Request request = new Request.Builder()
    //         .url("http://192.168.50.16:4090/web/workflow/workflow_info?x=1")
    //         .method("POST", body)
    //         .addHeader("Cookie", "session_id=9c20271c7a9521866cb8f3a282ea2fd6a8323495; csrftoken=fgCq0MUAvhuneuKXpaA7QDfyh93dV0hx4A9Vk8keDI9wZaRqpb30bQI5vNr3FzSa;")
    //         .addHeader("Content-Type", "application/json")
    //         .build();
    //     Response response = client.newCall(request).execute();
    //     return "ok";
    // }

//     public static void main(String[] args) throws IOException {
//         String b = "{\"jsonrpc\": \"2.0\",\"method\": \"call\",\"params\": {\"model\": \"ps.workflow.server\",\"method\": \"p_exec_services\", " +
//                 "\"data\": {\"bill_id\": 8132,\"services\": [{\"function\": \"svc_std_pre_audit\",\"model\": \"studio.base\"" +
//                 "}],\"model_id\": \"iv.input.vat.inv9\"},\"id\": null}";
//         System.out.println(getHtmlByPost("http://192.168.50.16:4090/web/workflow/workflow_info",b));
// //        String url="输入网址";
// //        String postBody="输入body参数，postman -》body里面直接复制就行";
// //        try{
// //            System.out.println(getHtmlByPost(url,postBody));
// //        } catch (IOException e) {
// //            e.printStackTrace();
// //        }
//         return;
//     }

    public static String getMAC(String apiRoute, String secretKey, long timestamp, String jsonStr, String method) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException{
		System.out.println("json is " + jsonStr);
		String secret = String.format("ts@%s|route@%s|sec@%s|method@%s", timestamp, apiRoute, secretKey, method);
		System.out.println("secret is " + secret);
        byte[] secBytes = secret.getBytes("UTF-8");
        MessageDigest mdSHA512 = MessageDigest.getInstance("SHA-512");
        mdSHA512.update(secBytes);
        byte[] macKey = mdSHA512.digest();
        System.out.println("key is " + new BigInteger(1, macKey).toString(16));
        byte[] bodyBytes = jsonStr.getBytes("utf-8");
        SecretKeySpec macKeySpec = new SecretKeySpec(macKey, "HmacSHA256");
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        sha256_HMAC.init(macKeySpec);
        byte[] hash = sha256_HMAC.doFinal(bodyBytes);
        String sign = new BigInteger(1, hash).toString(16);
		System.out.println("sign is "+ sign);
        return sign;
    }


    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException{

		String jsonString = IOUtils.objectToJson(IOUtils.fileToObject("src/resources/入参.json"));
        long timestamp = 1701325760L;
        String secretKey = "KBfP5FQuZKdrUp5e3zaz6nqe7dWsFwxc";
        String apiRoute = "/studio/api_special/insuite/zw/wbdj";
        String sign = getMAC(apiRoute, secretKey, timestamp, jsonString, "post");
        return;
    }
}
